package TUILayer;

import java.util.Scanner;
import ControlLayer.LoanCtr;
/**
 * This class is part of an address book system. It's the Loan part of the system UI. This menu is associated with the MainMenuUI class. 
 *
 * The purpose of this class is to communicate with the user and send the requests to the controllayer. 
 * 
 * @author Anders A.P, Anna K., Danny M.P., Jakob K., Mie P.  
 * @version 1.0
 */
public class LoanUI
{
    private LoanCtr loanCtr;

    /**
     * Constructs an instance of LoanUI, taking no parameters.
     */
    public LoanUI()
    {
        loanCtr = new LoanCtr();
    }

    /**
     * Initiates the LoanUI class.
     */
    public void start()
    {
        loanMenu();
        loanCtr = new LoanCtr();
    }

    /**
     * Starts the UI system in the loan menu, allowing the user to move through the parts of the menu.
     */ 
    private void loanMenu()
    {
        boolean exit = false;
        while(!exit) {
            int choice = writeLoanMenu();
            switch (choice) {
                case 1:
                    startCreateLoan();
                    break;
                case 2:
                    startReturnDVD();
                    break;
                case 3:
                    startListLoans();
                    break;
                case 4:
                    exit = true;
                    break;
                default:
                    System.out.println("Command not known");
                    break;
            }
        }
    }

    /**
     * Initiates the text in the LoanUI, scanning the user inputs and returns the choice to the caller.
     *
     * @return  choice  The users choice of menu.
     */
    private int writeLoanMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Loan Menu ***");
        System.out.println(" (1) Create a loan");
        System.out.println(" (2) Register return of DVD");
        System.out.println(" (3) List loan history");
        System.out.println(" (4) Return to main menu");
        System.out.print("\n  Make your choice: ");

        int choice = inputChoice();
        return choice;
    }

    /**
     * Method for reading the choice, for when the user wants to navigate the menu. Only accepts integer numbers.
     */
    private int inputChoice()
    {
        boolean valid = false;
        int no = 0;
        while(!valid)
        {
            Scanner keyboard = new Scanner(System.in);
            try {
                no = keyboard.nextInt();
                valid = true;
            }
            catch(Exception e) {
                System.out.println("Input must only be numbers!");
                waitInput();
            }
        }
        return no;
    }

    /**
     * Method for getting input to create a loan.
     */
    private void startCreateLoan()
    {
        String id = inputString("Type the Id of the loaner:");
        String title = inputString("Type the title of the DVD:");
        String fromDate = inputString("Type the start date of the loan:");
        String toDate = inputString("Type the loans returndate:");
        try {
            loanCtr.createLoan(id, title, fromDate, toDate);
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            waitInput();
        }
    }

    /**
     * Method for getting input to register the return of a loan.
     */
    private void startReturnDVD()
    {
        try {
            String  loanerName = inputString("Type the name of the loaner:");
            System.out.print(loanCtr.findLoan(loanerName) + "\n \n");

            int     loanId = inputInt("Type the Loan ID:");
            boolean isTooLate = inputYesNo("Is the DVD returned too late?");
            loanCtr.returnDVD(loanId, isTooLate); // DEMO call added
        }
        catch (Exception e) {
            System.out.println(e);
            waitInput();
        }            
    }

    /**
     * Method for printing a list of all loans.
     */
    private void startListLoans()
    {
        System.out.print("******** Loan History ******** \n");
        System.out.println(loanCtr.listAll());      
        waitInput();
    }

    /**
     * Method for reading a String as input.
     * 
     * @param   s   The text presented to the user.
     * @return  i   A string as typed by the user.
     */
    private String inputString(String s)
    {   
        Scanner keyboard = new Scanner(System.in);
        System.out.println(s);
        //String i = keyboard.next(); // keyboard.next() only returnes the first word of what the user typed in.
        String i = keyboard.nextLine();
        return i;
    }

    /**
     * Method for reading an integer as input.
     * Only integer numbers are accepted.
     * 
     * @param   s   The text presented to the user.
     * @return  no  The input integer if it is validated.
     */
    private int inputInt(String s)
    {
        boolean valid = false;
        int no = 0;
        while(!valid)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println(s);
            try {
                no = keyboard.nextInt();
                valid = true;
            }
            catch(Exception e) {
                System.out.println("Input must only be numbers.");
                waitInput();
            }
        }
        return no;
    }

    /**
     * Method for validating yes or no as input.
     * Only y/Y or n/N are accepted.
     * 
     * @param   s   The text presented to the user.
     * @return  A   boolean expression. Yes = true, no = false.
     * 
     */
    private boolean inputYesNo(String s)
    {
        boolean valid = false;
        boolean yesNo = true;
        while(!valid)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println(s + "\n" + "(y) Yes \n" + "(n) No \n"); // The text presented to the user.
            String userInput = keyboard.next().trim().toLowerCase();
            //if(keyboard.next().trim().toLowerCase().equals("y")) { //DEMO change
            if(userInput.equals("y")){
                valid = true;
                yesNo = true;
            }
            //else if(keyboard.next().trim().toLowerCase().equals("n")) { //DEMO change
            else if(userInput.equals("n")){
                valid = true;
                yesNo = false;
            }
            else {
                System.out.println("Input must only be y or n!");
                waitInput();
            }
        }
        return yesNo;
        
    }

    /**
     * Method to wait for user input.
     */
    private void waitInput()
    {
        System.out.println("Hit enter to continue"); //DEMO
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
}
